package dominio.camadaneural.especializacao;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import dominio.camadaneural.CamadaNeural;
import dominio.neuronio.Neuronio;
import dominio.padrao.Padrao;

public class CamadaNeuralFeedForward implements CamadaNeural{

	// lista de neuronios da camada
	private List<Neuronio> neuronios = new ArrayList<Neuronio>();
	
	private double[] estimulos;
	private double[] saidasCamada;

	/* (non-Javadoc)
	 * @see dominio.redeneural.CamadaNeural#adicionarNeuronio(dominio.neuronio.Neuronio)
	 */
	public void adicionarNeuronio( Neuronio neuronio ) {
		this.neuronios.add( neuronio );
	}
	
	/* (non-Javadoc)
	 * @see dominio.camadaneural.CamadaNeural#removerNeuronio(dominio.neuronio.Neuronio)
	 */
	public boolean removerNeuronio( Neuronio neuronio ) {
		return this.neuronios.remove( neuronio );
	}

	/* (non-Javadoc)
	 * @see dominio.redeneural.CamadaNeural#estimularCamada(double[])
	 */
	public double[] estimularCamada(Padrao padrao) {
		
		// gaurda uma cópia para o toString
		this.estimulos = Arrays.copyOfRange(padrao.getEstimulos(), 0, padrao.getQtdeEstimulos());
		
		double saidasCamadaNeural[] = new double[this.neuronios.size()];
		
		int aux = 0;
		for( Neuronio neuronio : this.neuronios ) {
			saidasCamadaNeural[aux++] = neuronio.estimular( padrao );
		}
		
		//
		this.saidasCamada = Arrays.copyOfRange(saidasCamadaNeural, 0, saidasCamadaNeural.length);
		
		return saidasCamadaNeural;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		DecimalFormat formatter = new DecimalFormat( "0.0000" );
		formatter.setPositivePrefix( " " );
		
		StringBuffer buf = new StringBuffer();
		buf.append( "\tCamada Neural Feed Forward **************************************************\n\n" );
		
		for (Neuronio neuronio : this.neuronios) {
			buf.append( neuronio.toString() );
		}
		
		buf.append( "\tResumo da Camada Neural *****************************************************\n" );
		buf.append( "\tEstimulos: ");
		if( this.estimulos != null ){
			for (int i = 0; i < this.estimulos.length; i++) {
				buf.append( formatter.format( this.estimulos[i] ) ).append( i < this.estimulos.length - 1 ? ", " : "\n" );
			}
		}else{
			buf.append( "NENHUM" ).append( "\n" );
		}
		buf.append( "\tSaídas da camada: ");
		if(this.saidasCamada != null){
			for (int i = 0; i < this.saidasCamada.length; i++) {
				buf.append( formatter.format( this.saidasCamada[i] ) ).append( i < this.saidasCamada.length - 1 ? ", " : "\n\n" );
			}
		}else{
			buf.append( "NENHUM" ).append( "\n" );
		}
		buf.append( "\t*****************************************************************************\n\n" );
		return buf.toString();
	}

	/* (non-Javadoc)
	 * @see dominio.camadaneural.CamadaNeural#getNeuronios()
	 */
	public List<Neuronio> getNeuronios() {
		return this.neuronios;
	}

}
